<?php
/**
 http://ip.zxinc.org/ 使用该地址库
 要求：php版本为64位 5.6+以上版本
 支持ipv4和ipv6查询
 (C) 2020 - 2025 保留所有权利
*/ 
namespace Ipnew;

require("ipdbv4.func.php");
require("ipdbv6.func.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/**
* 
*/
class Ipnew
{
	const IPV4DBPATH='ipv4wry.db';
	const IPV6DBPATH='ipv6wry.db';

	function __construct()
	{
		
	}
	public static function query($ip){
		try {
			if ($ip == "") {
				$result = array("disp" => "请输入IP");
			} else if (self::isipv6($ip)) {
				$db6 = new ipdbv6(dirname(__FILE__).'/'.self::IPV6DBPATH);
				$result = $db6->query($ip, $db4);
			} else {
				$db4 = new ipdbv4(dirname(__FILE__).'/'.self::IPV4DBPATH);
				$result = $db4->query($ip);
			}
		} catch (\Exception $e) {
			$result = array("disp" => $e->getMessage());
			return false;
		}
		$i1 = $result["start"];
		$i2 = $result["end"];
		$disp = $result["disp"];
		$o1 = $result["addr"][0];
		$o2 = $result["addr"][1];

		$disp = str_replace("\"", "\\\"", $disp);
		$o1 = str_replace("\"", "\\\"", $o1);
		$o2 = str_replace("\"", "\\\"", $o2);
		
		return array(
			'myip'=>$ip,
			'country'=>$o1
		);
	}
	/*
	判断是否为ipv6
	*/
	public static function isipv6($s) {
		return strpos($s, ":") !== false;
	}
}